package com.example.hasee.usersetting;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hasee.data.entity.Student;
import com.example.hasee.libraryreservation.R;
import com.example.hasee.utils.NetUtils;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;


public class UserSettingFragment extends Fragment implements UserSettingContract.View{
    private TextView tvName,tvSid,tvGrade;
    private ImageView imgIcon;
    private EditText edtSid,edtGrade;
    private Button btnOk;
    private UserSettingContract.Presenter mPresenter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user_setting, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvName=view.findViewById(R.id.tvName);
        tvSid=view.findViewById(R.id.tvSid);
        tvGrade=view.findViewById(R.id.tvGrade);
        imgIcon=view.findViewById(R.id.imgIcon);
        edtSid=view.findViewById(R.id.edtSid);
        edtGrade=view.findViewById(R.id.edtGrade);
        btnOk=view.findViewById(R.id.btnOk);
        mPresenter=new UserSettingPresenter(this);
        mPresenter.getStudentInfo();
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String sid=edtSid.getText().toString().trim();
                final String grade=edtGrade.getText().toString().trim();
                mPresenter.updateStudentInfo(sid,grade);
                edtSid.setText("");
                edtGrade.setText("");
            }
        });
    }

    @Override
    public void showStudentInfo(Student student) {
        tvName.setText(student.getUsername());
        tvSid.setText("学号:"+student.getSid());
        tvGrade.setText("班级:"+student.getGrade());
        //开启异步任务
        new DownImageTask().execute();
    }
    private class DownImageTask extends AsyncTask<Void,Void,Bitmap>{
        @Override
        protected Bitmap doInBackground(Void... voids) {
            //创建Url
            URL url= null;
            try {
                url = new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1546140105934&di=f07ec59f0f694415f5dc36cbaa4ffba0&imgtype=p0&src=http%3A%2F%2Fpic5.nipic.com%2F20100118%2F3356394_144512059655_2.jpg");
                //把url传递给工具类，返回数据输入流
                InputStream inputStream=NetUtils.getNetData(url);
                //把输入流转为Bitmap对象
                Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
                return bitmap;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            imgIcon.setImageBitmap(bitmap);
        }
    }

}
